package com.heima.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleService;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.Article;
import com.heima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author t_star
 */
@Slf4j
@Service
@Transactional
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 文章列表
     * @param dto 查询条件
     * @param type 1 表示查询更新的数据 2 表示查询更多
     * @return
     */
    @Override
    public ResponseResult loadArticleList(ArticleHomeDto dto, Short type) {
        //1. 数据校验
        if (type == null){
            type = 1;
        }
        if (null == dto.getSize() && dto.getSize()==0){
            dto.setSize(10);
        }
        dto.setSize(Math.min(dto.getSize(),30));
        if (StringUtils.isBlank(dto.getTag())){
            dto.setTag("__all__");
        }
        if (null == dto.getMaxBehotTime() && dto.getMinBehotTime()== null){
            dto.setMaxBehotTime(new Date());
            dto.setMinBehotTime(new Date(0));
        }
        //2.调用mapper 返回数据
        return ResponseResult.okResult(articleMapper.loadArticleList(dto,type));
    }
}
